<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item><a href="#/">首页</a></el-breadcrumb-item>
      <el-breadcrumb-item><a href="#/">商品列表</a></el-breadcrumb-item>
    </el-breadcrumb>
    <el-table
      :data="products"
      style="width: 100%">
      <el-table-column
        prop="title"
        label="商品">
      </el-table-column>
      <el-table-column
        prop="price"
        label="价格">
      </el-table-column>
      <el-table-column
        prop="address"
        label="操作">
        <!-- 使用作用域插槽获取插槽数据/props   -->
        <!-- Vue2.6以前使用 slot-scope="scope" scope是一个对象 其中的row属性就是绑定的数据 -->
        <!-- <template slot-scope="scope"> -->
          <!-- vue2.6以后 使用v-slot="scope" 使用scope.row获取当前行绑定的商品数据 -->
        <template v-slot="scope">
          <el-button @click="addToCart(scope.row)">加入购物车</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { mapState, mapActions, mapMutations } from 'vuex'
export default {
  name: 'ProductList',
  computed: {
    ...mapState('products', ['products'])
  },
  methods: {
    ...mapActions('products', ['getProducts']),
    ...mapMutations('cart', ['addToCart'])
  },
  // 组件创建完毕时钩子函数
  created () {
    this.getProducts()
  }
}
</script>

<style></style>
